package com.bdqn.crm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.crm.mapper.SysUserMapper;
import com.bdqn.crm.pojo.SysUser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserMapperTester {
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 用ID查询
     */
    @Test
    public void testSelectById(){
        SysUser sysUser=sysUserMapper.selectById(10L);
        System.out.println("usrName:"+sysUser.getUsrName());
    }

    /**
     * 新增
     */
    @Test
    public void testInsert(){
        sysUserMapper.insert(new SysUser("MyBatis-Plus","123456",4L,1));
    }
    /**
     * 查询所有
     */
    @Test
    public void testFindAll(){
        List<SysUser>list=sysUserMapper.selectList(null);
        for (SysUser sysUser : list) {
            System.out.println("usrName:"+sysUser.getUsrName());
        }
    }

    /**
     * 条件查询:根据用户名和密码查询
     */
    @Test
    public void testFind(){
        QueryWrapper<SysUser>wrapper=new QueryWrapper<>();
        wrapper.eq("usr_name","MyBatis-Plus");
        wrapper.eq("usr_password","123456");
        List<SysUser>list=sysUserMapper.selectList(wrapper);
        if(list!=null){
            for (SysUser sysUser : list) {
                System.out.println("usrId:"+sysUser.getUsrId());
            }
        }
    }
@Test
    //在Junit测试类UserMapperTester.java中添加按角色进行分页查询的测试方法
    public void testPageination(){//分页查询
        QueryWrapper<SysUser>wrapper=new QueryWrapper<>();
        wrapper.eq("usr_role_id",2L);//查询条件:角色Id为2
        //分页对象:查询获得User对象，查询第1页数据，每页显示2条记录
        Page<SysUser>page=new Page<>(1,2);
        IPage<SysUser> userPager=sysUserMapper.selectPage(page,wrapper);
        System.out.println("总记录数---------->"+userPager.getTotal());
        System.out.println("总页数------------>"+userPager.getPages());
        System.out.println("当前页数----------->"+userPager.getCurrent());
        System.out.println("每页记录数---------->"+userPager.getSize());
        System.out.println("当前记录:----------->");
        for (SysUser sysUser:userPager.getRecords() ) {
            System.out.println("usrId:"+ sysUser.getUsrId());
        }

    }

}
